package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.controller.BascStudentController;
import com.itheima.edu.info.manager.domain.Student;

import java.awt.*;
import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    //创建对象数组
   private static ArrayList<Student> studs = new ArrayList<>();

     static {
       studs.add(new Student("it001","张三","23","2000-12-12"));
       studs.add(new Student("it002","李四","24","1999-12-12"));

   }
   //得到对象添加
    public boolean addStudent(Student stu) {

        int index = -1;
        //遍历数组是否有null
        for (int i = 0; i < studs.size(); i++) {
            if (studs.get(i)==null){
                index = i;
                break;
            }
        }

            return studs.add(stu);
    }
//取出所有对象
    public Student[] findAllStudent() {
         Student[] students =studs.toArray(new Student[studs.size()]);
        return students;
    }
//删除对象
    public void deleteStudentById(String delId) {
        int index = getIndex(delId);
        studs.remove(index);


    }
    //获取索引
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < studs.size(); i++) {
            Student stu = studs.get(i);
            if (stu !=null&&stu.getId().equals(id)){
                index =i;
                break;
            }

        }
        return index;

    }
//修改
    public void updateStudent(String updateId, Student newStu) {
        int index = getIndex(updateId);
        studs.set(index,newStu);
    }
}

